package org.lq.ht.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.ht.dao.GoodsDao;
import org.lq.ht.entity.Goods;
import org.lq.ht.util.Instantiation;
import org.lq.ht.util.JDBCUtil;
/**
 * 商品数据访问层接口实现
 *@author 张冲
 * @qq2865346830
 * @package org.lq.ht.dao.impl
 * @date 2020年8月21日下午8:03:30
 */
public class GoodsDaoImpl  implements GoodsDao ,Instantiation<Goods>{

	@Override
	public int save(Goods t) {
		String sql = "insert into goods (number,name,spell,purchase_price,sell_price,type,inventory,max_inventory,min_inventory,integral,supplier,note,unit) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		int num=0;
		try {
			num=JDBCUtil.executeUpdate(sql, t.getNumber(),t.getName(),t.getSpell(),t.getPurchase_price(),t.getSell_price(),t.getType(),t.getInventory(),t.getMax_inventory(),t.getMin_inventory(),t.getIntegral(),t.getSupplier(),t.getNote(),t.getUnit());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int update(Goods t) {
		String sql = "update goods set number=?,name=?,spell=?,purchase_price=?,sell_price=?,type=?,inventory=?,max_inventory=?,min_inventory=?,integral=?,supplier=?,note=?,unit=? where no=?";
		int num=0;
		try {
			num=JDBCUtil.executeUpdate(sql, t.getNumber(),t.getName(),t.getSpell(),t.getPurchase_price(),t.getSell_price(),t.getType(),t.getInventory(),t.getMax_inventory(),t.getMin_inventory(),t.getIntegral(),t.getSupplier(),t.getNote(),t.getNo(),t.getUnit());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int delete(int id) {
		String sql = "delete from goods where no =?";
		int num =0;
		try {
			num = JDBCUtil.executeUpdate(sql, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public List<Goods> findAll() {
		List<Goods> list = new ArrayList<Goods>();
		String sql = "select * from goods";
		try {
			list=JDBCUtil.executeQuery(sql, this );
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Goods getById(int id) {
		String sql = "select * from goods where no =?";
		List<Goods> list = new ArrayList<Goods>();
		try {
			list=JDBCUtil.executeQuery(sql, this, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list.get(0);
	}

	@Override
	public Goods instance(ResultSet rs) {
		Goods  g = new Goods();
		try {
			g.setNo(rs.getInt("no"));
			g.setNumber(rs.getString("number"));
			g.setName(rs.getString("name"));
			g.setSpell(rs.getString("spell"));
			g.setPurchase_price(rs.getDouble("purchase_price"));
			g.setSell_price(rs.getDouble("sell_price"));
			g.setType(rs.getInt("type"));
			g.setInventory(rs.getInt("inventory"));
			g.setMax_inventory(rs.getInt("max_inventory"));
			g.setMin_inventory(rs.getInt("min_inventory"));
			g.setIntegral(rs.getString("integral"));
			g.setSupplier(rs.getInt("supplier"));
			g.setNote(rs.getString("note"));
			g.setUnit(rs.getInt("unit"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return g;
	}

	@Override
	public List<Goods> getByName(String name) {
		String sql = "select * from goods where name like ?";
		try {
			return JDBCUtil.executeQuery(sql, this, "%"+name+"%");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Goods> getByPinYin(String name) {
		String sql = "select * from goods where spell like ?";
		try {
			return JDBCUtil.executeQuery(sql, this, "%"+name.toUpperCase()+"%");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Goods> getByNumber(String name) {
		String sql = "select * from goods where number like ?";
		try {
			return JDBCUtil.executeQuery(sql, this, "%"+name+"%");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public Goods getByNumber(int number){
		String sql = "select * from goods where number =?";
		List<Goods> list = new ArrayList<Goods>();
		try {
			list=JDBCUtil.executeQuery(sql, this, number+"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list.get(0);
	}


}

